<template>
  <el-container class="home-container">
      <!-- 头部区 -->
  <el-header>
      <div>
          <img src="../assets/img/a.jpg" class="logoImage" alt="">
          <span>电商后台管理系统</span>
      </div>
      <el-button @click="logOut" type="info">退出</el-button> 
  </el-header>
  <el-container>
      <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' :'200px'">
        <div class="toggle-btn" @click="toggleCollapse">|||</div>
        <el-menu
            background-color="#333744"
            text-color="#fff"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition ="false"
            :router="true"
            :default-active="activePath"
            active-text-color="#409eef">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
              <template slot="title">
                <i :class="isCollapse ?'el-icon-folder':'el-icon-folder-opened'"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
               <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="savePath('/'+subItem.path)">
                    <template slot="title">
                    <i class="el-icon-menu"></i>
                    <span>{{subItem.authName}}</span>
              </template>
               </el-menu-item>
            </el-submenu> 
        </el-menu>
    </el-aside>
    <!-- 主要内容 -->
    <el-main><router-view></router-view></el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
    data() {
        return {
            // 左侧菜单栏数据
            menulist:[],
            // 侧边栏是否打开
            isCollapse:false,
            // 被激活的链接地址
            activePath:'',

        }
    },
    created(){
        this.getMenuList();
        this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods:{
        // 退出
        logOut(){
            window.sessionStorage.clear();
            this.$router.push('/login')
        },
        // 获取侧边栏菜单列表
        async getMenuList(){
          const { data:res } = await this.$http.get('menus')
          if(res.meta.status !==200) return this.$message.console.error(res.meta.msg);
          this.menulist = res.data
        },
        // 侧边栏折叠与展开
        toggleCollapse(){
            this.isCollapse = !this.isCollapse;
        },
        //取得打开侧边栏索引值 
        savePath(activePath){
            window.sessionStorage.setItem('activePath',activePath)
            this.activePath = activePath
        }
    }
}
</script>

<style lang="less" scoped>
.home-container{
    height: 100%;
    .logoImage{
        width: 45px;
        height: 45px;
        border-radius: 50%;
    }
}
.el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 30px;
    align-items: center;
    color: #fff;
    font-size: 20px;
    div{
        display: flex;
        align-items: center;
        span{
            margin-left: 15px;
        }
    }
}
.el-aside{
    background-color: #333744;
    .el-menu{
        border-right: 0;
    }
}
.el-main{
    background-color: #eaedf1;
}
.toggle-btn{
    background-color: #69718b;
    font-size: 10px;
    line-height: 24px;
    color: rgb(255, 255, 255);
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>